<?php
declare (strict_types = 1);

namespace app\middleware;

use app\common\service\UserService\UserManageService;
use app\Normal;
class shopMiddileware
{
    /**
     * 排除不需要登录的模块
     */
    protected $except = [
        '/shop/Manager/Login',
        '/shop/Manager/Register',
        '/shop/Manager/ForgetPassWord',
    ];
    
    /**
     * master中间介处理登录验证问题
     * @param $request
     * @param \Closure $next
     */
    public function handle($request, \Closure $next)
    {
        $baseurl=$request->baseUrl();
        $urlarray= explode('/', $baseurl);
        $url='/'.$urlarray[1].'/'.$urlarray[2].'/'.$urlarray[3];
        //如果出现APP_DEBUG_USER_ID,则登录信息就是使用当前用户系统配置的USER_ID
        if( env('APP_DEBUG_USER_ID') ) {
            $user_id = env('APP_DEBUG_USER_ID');
            $manager= \app\model\BbcModel\Manager::instance()->info(['id'=>$user_id]);
            if(empty($manager)){
                $result = Normal::returnCode(Normal::NO_LOGIN,"配置用户未找到相关信息");
                return apiResponse($result);
            }
            $cid=$manager['cid'];
            $cb_id=$manager['cb_id'];
            $group_id=$manager['group_id'];
        } else {
            
            //如果不在排除登录的模块里面，则需要进行登录
            if(!in_array($url,$this->except)) {
                $token = $request->header('token');
                if( empty($token) ) {
                    $result = Normal::returnCode(Normal::NO_LOGIN,"该模块需要认证");
                    return apiResponse($result);
                }
                $destoken= callApi(UserManageService::class,'keyDecrypt',$token);
                if(!$destoken){
                   $result = Normal::returnCode(Normal::NO_LOGIN,"token无效"); 
                   return apiResponse($result);
                }
                $tokendata= explode('&', $destoken);
                $user_id=$tokendata[0];
                $cid=$tokendata[1];
                $cb_id=$tokendata[2];
                $group_id=$tokendata[3];
                $redis = getRedisObj();
                $user_token = $redis->get(UserManageService::master_login_cache_key.$tokendata[0]);
                if( empty($user_token)||$user_token!=$token) {
                    $result = Normal::returnCode(Normal::NO_LOGIN,"登录失效或未登录");
                    return apiResponse($result);
                }
                //更新token失效时间
                $tokendata[4]=time();
                $redis->set(UserManageService::master_login_cache_key.$tokendata[0],$token, UserManageService::master_login_expire_time);
            }
        }
       
        //如果有用户ID出现
        if( !empty($user_id) ) 
        {
            $result_query = callApi(UserManageService::class,'checkAuth',[
                'group_id'=>$group_id,
                'cid'=>$cid,
                'cb_id'=>$cb_id,
                'path'=> $url
            ]);
            if( $result_query['code'] != Normal::SUC ) {
                return apiResponse($result_query);
            }
            $data=[
                'manager_id'=>$user_id,
                'cid'=>$cid,
                'cb_id'=>$cb_id,
                'group_id'=>$group_id
            ];
            $request->offsetSet(UserManageService::master_system_user_key, $data);
        }
        return $next($request);
    }
}